﻿Imports System.Net
Imports System.IO
Imports System.Text
Namespace TWITPIC
    Public Class ClsTwitPic
        Public Function SendPicToTwitPic(ByVal userName As String, ByVal password As String, ByVal Message As String, ByVal ImgPath As String) As String

            Using fs As System.IO.FileStream = New FileStream(ImgPath, FileMode.Open, FileAccess.Read)
                Dim ImgData As Byte() = New Byte(fs.Length - 1) {}
                fs.Read(ImgData, 0, ImgData.Length)
                Return SendPicToTwitPic(userName, password, ImgData, Message)
            End Using
        End Function
        Private Function SendPicToTwitPic(ByVal UserName As String, ByVal PassWord As String, ByVal Image As Byte(), Optional ByVal Message As String = "", Optional ByVal Uri As String = "http://twitpic.com/api/uploadAndPost") As String
            Dim ret As String = String.Empty
            Dim request As HttpWebRequest = DirectCast(WebRequest.Create(Uri), HttpWebRequest)
            request.Method = "POST"
            request.Timeout = 30000

            If Not String.IsNullOrEmpty(UserName) AndAlso Not String.IsNullOrEmpty(PassWord) Then

                Dim boundary As String = System.Guid.NewGuid().ToString()

                request.Credentials = New NetworkCredential(UserName, PassWord)

                request.Headers.Add("Accept-Language", "en-us")
                request.PreAuthenticate = True
                request.ContentType = String.Format("multipart/form-data;boundary={0}", boundary)


                Dim header As String = String.Format("--{0}", boundary)
                Dim footer As String = header & "--"

                Dim contents As New StringBuilder()

                contents.Append(header)
                contents.Append(vbCrLf)
                contents.Append("Content-Disposition:form-data;name=""username""" & vbCrLf)
                contents.Append(vbCrLf)
                contents.Append(UserName)
                contents.Append(vbCrLf)

                contents.Append(header)
                contents.Append(vbCrLf)
                contents.Append("Content-Disposition:form-data;name=""password""" & vbCrLf)
                contents.Append(vbCrLf)
                contents.Append(PassWord)
                contents.Append(vbCrLf)

                If Not String.IsNullOrEmpty(Message) Then
                    contents.Append(header)
                    contents.Append(vbCrLf)
                    contents.Append("Content-Disposition:form-data;name=""message""" & vbCrLf)
                    contents.Append(vbCrLf)
                    contents.Append(Message)
                    contents.Append(vbCrLf)
                End If

                contents.Append(header)
                contents.Append(vbCrLf)
                contents.Append("Content-Disposition:form-data;name=""media"";filename=""image.jpg""" & vbCrLf)
                contents.Append("Content-Type: image/jpeg" & vbCrLf)
                contents.Append(vbCrLf)

                Dim Einde As String = vbCrLf & header & vbCrLf
                Dim BodyBytes As Byte() = Encoding.UTF8.GetBytes(contents.ToString())
                Dim footerBytes As Byte() = Encoding.UTF8.GetBytes(Einde)

                request.ContentLength = BodyBytes.Length + Image.Length + footerBytes.Length()

                Using requestStream As Stream = request.GetRequestStream()
                    requestStream.Write(BodyBytes, 0, BodyBytes.Length)
                    requestStream.Write(Image, 0, Image.Length)
                    requestStream.Write(footerBytes, 0, footerBytes.Length)
                    requestStream.Flush()
                    requestStream.Close()

                    Using response As WebResponse = request.GetResponse()
                        Using reader As New StreamReader(response.GetResponseStream())
                            ret = reader.ReadToEnd()
                        End Using
                    End Using
                End Using
            End If

            Return ret
        End Function

    End Class

End Namespace
